module UsersHelper
  def has_logined_by_user?
    Thread.current[:current_user_id]
  end

  def has_logined_by?(user_or_id)
    user_or_id.try(:id) == current_user_id || user_or_id == current_user_id
  end

  def has_logined_by_admin?
    current_user.try :admin?
  end

  def current_user
    User.find(current_user_id) if current_user_id
  end

  def current_user_id
    session[:current_user_id]
  end
  
  def column_id setting
    case setting.column
    when 1
      'left_portalets'
    when 2
      'mid_portalets'
    when 3
      'right_portalets'
    else
      raise "未知栏目#{setting.column}"
    end
  end
  
  def link_to_add_fetion_if_setting_user_no_mobile setting
    link_to '尚未绑定手机，赶紧！', 
      edit_user_url(:id => setting.user.id, :target => :mobile), 
      :class => :alert,
      :target => :_blank, 
      :title => '不绑定手机，将无法收到短信哦',
      :id => "#{setting.id}_no_mobile" if setting.user.try(:mobile).try(:blank?)
  end

  def link_to_add_fetion_if_user_no_mobile user
    link_to '尚未绑定手机，赶紧！', 
      edit_user_url(:id => user.id, :target => :mobile), 
      :class => :alert,
      :target => :_blank, 
      :title => '不绑定手机，将无法收到短信哦',
      :id => "#{user.id}_no_mobile" if user.try(:mobile).try(:blank?)
  end
  
  def introduce_url
    "http://www.mytusi.com/?introducer_id=#{current_user_id}"
  end
  
  def encode_introduce_url
    "http%3A%2F%2Fwww.mytusi.com%2F%3Fintroducer_id%3D#{current_user_id}"
  end
end
